// pages/mine/wxscan/supply_confirm.js
const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderNumber:'', //订单号
    showPhone:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '扫码处理中',
    })
    //扫码进来https://www-cxw.juglz.com/snackConfirm?order_no=MALLORDER201906181831225251201&type=snack
    const that = this;
    if (options.q) {
      let qrUrl = decodeURIComponent(options.q);//获取二维码的携带的链接信息
      let str = qrUrl.split('?')[1].split('&')[0];
      that.setData({
        orderNumber: str.substring(9)
      })
    } else if(options.order_no!=undefined){
      that.setData({
        orderNumber: options.order_no
      })
    }
    
    wx.login({
      success: function (res) {
        //发送请求
        wx.request({
          url: App.data.globalAPI + '/users/weixin/login', //接口地址
          data: {
            code: res.code,
            source: 'small program',//注册来源标识
            invite_id: ''//邀请人标识
          },
          header: {
            'content-type': 'application/json' //默认值
          },
          success: function (res) {
            if (res.data.error == 0) {
              wx.setStorageSync('token', res.data.data.token);
              wx.setStorageSync('userInfo', res.data.data.userinfo);
              wx.setStorageSync('allUserMsg', res.data.data);
              wx.hideLoading();
              if (res.data.data.userinfo.phone == '0') {
                that.setData({
                  showPhone: true
                })
              }else{
                wx.redirectTo({
                  url: '../order/order_detail?order_no=' + that.data.orderNumber
                })
              }
            }
          }
        })
      }
    })
   
  },

  //授权手机号
  getPhoneNumber: function (e) {
    let that = this;

    if (e.detail.errMsg == 'getPhoneNumber:ok') {//同意手机号授权
      wx.request({
        url: App.data.globalAPI + '/users/weixin/decrypt',
        data: {
          token: wx.getStorageSync('token'),
          data: e.detail.encryptedData,
          iv: e.detail.iv
        },
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success(res) {
          if (res.data.error == 0) {
            var myphone = JSON.parse(res.data.data).phoneNumber;

            let userInfo = wx.getStorageSync('userInfo');
            let allUserMsg = wx.getStorageSync('allUserMsg');
            userInfo.phone = myphone;
            allUserMsg.userinfo = userInfo;
            wx.setStorageSync('userInfo', userInfo);
            wx.setStorageSync('allUserMsg', allUserMsg);

            that.uploadUserMsg();
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 800
            })
          }
        }, fail() {
          wx.showModal({
            title: '提示',
            content: '网络异常，请稍后重试！',
            showCancel: false,
            success(res) {
            }
          })
        }
      })
    }
  },

  //提交用户手机
  uploadUserMsg: function () {
    let that = this;
    wx.request({
      url: App.data.globalAPI + '/users/user/bindphone',
      data: {
        token: wx.getStorageSync('token'),
        phone: wx.getStorageSync('userInfo').phone,
        validate_code: '0000',
        is_activity: 0
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success(res) {
        const arr = res.data;
        if (arr.error != 0) {
          wx.showToast({
            title: arr.msg,
            icon: 'none',
            duration: 800
          })
        }

        if (Object.keys(arr.data).length > 0) { //以前有盒子
          wx.showToast({
            title: '信息同步成功',
            icon: 'success',
            duration: 1000
          })
        } 
        setTimeout(function(){
          wx.redirectTo({
            url: '../order/order_detail?order_no=' + that.data.orderNumber
          })
        },500)
      }, fail() {
        wx.showModal({
          title: '提示',
          content: '网络异常，请稍后重试！',
          showCancel: false,
          success(res) {
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})